import type {
  InterviewQuestion,
  InterviewQuestionForm,
  InterviewQuestionQuery,
} from './model';

import type { ID, IDS, PageResult } from '#/api/common';

import { commonExport } from '#/api/helper';
import { requestClient } from '#/api/request';

enum Api {
  QUESTION_ADD = '/interview/question/add',
  QUESTION_CHANGE_STATUS = '/interview/question/changeStatus',
  QUESTION_DELETE = '/interview/question/delete',
  QUESTION_DIFFICULTY_UPDATE_ALL = '/interview/question/updateDifficultAll',
  QUESTION_EXPORT = '/interview/question/export',
  QUESTION_INFO = '/interview/question/info',
  QUESTION_LIST = '/interview/question/listWithStatus',
  QUESTION_UPDATE = '/interview/question/update',
  QUESTIONS_COMPLETE_BY_AI = '/interview/question/completeQuestionsByAi', // AI完善面试题
  QUESTIONS_COMPLETE_BY_AI_SSE = '/interview/question/completeQuestionsByAiSse', // AI完善面试题(SSE)
  QUESTIONS_GET_BY_AI = '/interview/question/getQuestionsByAi', // AI获取面试题
}

// 查询面试题列表
export function getQuestionList(params: InterviewQuestionQuery) {
  return requestClient.get<PageResult<InterviewQuestion>>(Api.QUESTION_LIST, {
    params,
  });
}

// 查询面试题详细信息
export function getQuestionInfo(questionId: ID) {
  return requestClient.get<InterviewQuestion>(Api.QUESTION_INFO + questionId);
}

// 新增面试题
export function addQuestion(data: InterviewQuestionForm) {
  return requestClient.post(Api.QUESTION_ADD, data);
}

// 修改面试题
export function updateQuestion(data: InterviewQuestionForm) {
  return requestClient.put(Api.QUESTION_UPDATE, data);
}

// 删除面试题
export function delQuestion(questionId: IDS) {
  return requestClient.delete(Api.QUESTION_DELETE + questionId);
}

// 导出面试题
export function exportQuestion(params: InterviewQuestionQuery) {
  return commonExport(Api.QUESTION_EXPORT, params);
}

// 修改面试题状态
export function changeQuestionStatus(questionId: ID, status: string) {
  return requestClient.put(Api.QUESTION_CHANGE_STATUS, {
    questionId,
    status,
  });
}

// 更新所有面试题难度
export function updateAllQuestionDifficulty() {
  return requestClient.post(Api.QUESTION_DIFFICULTY_UPDATE_ALL);
}

// 通过AI获取面试题建议
export function getQuestionsByAi(data: { categoryId: number; prompt: string }) {
  return requestClient.post<{ code: number; data: any[]; msg: string }>(
    Api.QUESTIONS_GET_BY_AI,
    data,
    {
      timeout: 30_000, // 设置超时时间为30秒
    },
  );
}

// 通过AI完善面试题
export function completeQuestionsByAi(data: {
  categoryId: number;
  questions: any[];
}) {
  return requestClient.post<{ code: number; data: any[]; msg: string }>(
    Api.QUESTIONS_COMPLETE_BY_AI,
    data,
    {
      timeout: 60_000, // 设置超时时间为60秒
    },
  );
}

// 通过AI完善面试题 (SSE版本)
export function completeQuestionsByAiSse(data: {
  categoryId: number;
  questions: any[];
}) {
  return requestClient.post<{ code: number; data: any[]; msg: string }>(
    Api.QUESTIONS_COMPLETE_BY_AI_SSE,
    data,
    {
      timeout: 0, // SSE 不设置超时
    },
  );
}